package com.video.information.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.video.information.R
import com.video.information.extension.setOnClickListener
import com.video.information.ui.common.ui.BaseActivity
import com.video.information.ui.home.HomeFragment
import com.video.information.ui.information.InformationFragment
import com.video.information.ui.my.MyFragment
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_bottom_navigation_bar.*

class MainActivity : BaseActivity() {

    companion object {
        const val HOME_FRAGMENT = 0
        const val INFORMATION_FRAGMENT = 1
        const val MY_FRAGMENT = 2
        fun start(context: Context) {
            context.startActivity(Intent(context, MainActivity::class.java))
        }
    }

    private val fragments = ArrayList<Fragment>()

    private var fragmentType = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        setStatusBarBackground(R.color.barThemeColor)
    }

    override fun setupViews() {
        fragments.add(HomeFragment.newInstance())
        fragments.add(InformationFragment.newInstance())
        fragments.add(MyFragment.newInstance())
        viewpager.offscreenPageLimit = fragments.size
        viewpager.adapter = MainPagerAdapter(supportFragmentManager, fragments)
        viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                setTabSelection(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })

        setOnClickListener(btnHome, btnInformation, btnMy) {
            when (this) {
                btnHome -> {
                    viewpager.currentItem = HOME_FRAGMENT
                    setTabSelection(HOME_FRAGMENT)
                }
                btnInformation -> {
                    viewpager.currentItem = INFORMATION_FRAGMENT
                    setTabSelection(INFORMATION_FRAGMENT)
                }
                btnMy -> {
                    viewpager.currentItem = MY_FRAGMENT
                    setTabSelection(MY_FRAGMENT)
                }
            }
        }
        setTabSelection(HOME_FRAGMENT)
    }

    private fun setTabSelection(index: Int) {
        if (index == fragmentType) return
        clearAllSelected()
        when(index) {
            HOME_FRAGMENT -> {
                btnHome.isSelected = true
                ivHome.setImageResource(R.drawable.home_selected)
            }
            INFORMATION_FRAGMENT -> {
                btnInformation.isSelected = true
                ivInformation.setImageResource(R.drawable.information_selected)
            }
            MY_FRAGMENT -> {
                btnMy.isSelected = true
                ivMy.setImageResource(R.drawable.my_selected)
            }
        }
        when(fragmentType) {
            HOME_FRAGMENT -> {
                ivHome.setImageResource(R.drawable.home_unselect)
            }
            INFORMATION_FRAGMENT -> {
                ivInformation.setImageResource(R.drawable.information_unselect)
            }
            MY_FRAGMENT -> {
                ivMy.setImageResource(R.drawable.my_unselect)
            }
        }
        fragmentType = index
    }

    private fun clearAllSelected() {
        btnHome.isSelected = false
        btnInformation.isSelected = false
        btnMy.isSelected = false
    }
}